package com.tqz.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tqz.dao.NewsSerch;
import com.tqz.dao.NewsSerch_army;
import com.tqz.dao.NewsSerch_entertainment;
import com.tqz.dao.NewsSerch_incountry;
import com.tqz.dao.NewsSerch_outcountry;
import com.tqz.dao.NewsSerch_society;
import com.tqz.dao.NewsSerch_sports;
import com.tqz.dao.NewsSerch_tech;

@WebServlet("/AddNewsServlet")
public class AddNewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public AddNewsServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 防止中文乱码，设置字符集
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charSet=utf-8");
		String title = "default";
		String content = "default";
		int weight = 0;
		int type = 0;
		title = request.getParameter("title");
		content = request.getParameter("content");
		weight = Integer.valueOf(request.getParameter("weight"));
		type = Integer.valueOf(request.getParameter("type"));// type是要写入数据库的新闻类型，根据不同类型传入不同的表
		int affect = 0;
		String repath = request.getContextPath();// 获取当前路径上下文
		switch (type) {
		case 0: {
			NewsSerch newsSerch = new NewsSerch();
			affect = newsSerch.addArticle(title, content, weight); // affect表示影响的行数
			response.sendRedirect(repath + "/optionnewssuccess.jsp");
		}
			break;
		case 1: {
			NewsSerch_incountry newsSerch = new NewsSerch_incountry();
			affect = newsSerch.addArticle(title, content, weight);
			response.sendRedirect(repath + "/optionnewssuccess.jsp");
		}
			break;
		case 2: {
			NewsSerch_outcountry newsSerch = new NewsSerch_outcountry();
			affect = newsSerch.addArticle(title, content, weight);
			response.sendRedirect(repath + "/optionnewssuccess.jsp");
		}
			break;
		case 3: {
			NewsSerch_society newsSerch = new NewsSerch_society();
			affect = newsSerch.addArticle(title, content, weight);
			response.sendRedirect(repath + "/optionnewssuccess.jsp");
		}
			break;
		case 4: {
			NewsSerch_army newsSerch = new NewsSerch_army();
			affect = newsSerch.addArticle(title, content, weight);
			response.sendRedirect(repath + "/optionnewssuccess.jsp");
		}
			break;
		case 5: {
			NewsSerch_tech newsSerch = new NewsSerch_tech();
			affect = newsSerch.addArticle(title, content, weight);
			response.sendRedirect(repath + "/optionnewssuccess.jsp");
		}
			break;
		case 6: {
			NewsSerch_sports newsSerch = new NewsSerch_sports();
			affect = newsSerch.addArticle(title, content, weight);
			response.sendRedirect(repath + "/optionnewssuccess.jsp");
		}
			break;
		case 7: {
			NewsSerch_entertainment newsSerch = new NewsSerch_entertainment();
			affect = newsSerch.addArticle(title, content, weight);
			response.sendRedirect(repath + "/optionnewssuccess.jsp");
		}
			break;
		default:
			System.out.println("error!");
			break;
		}
	}
}
